
#include "..\headers\ListOf.h"

#define MAX_SIZE 100

typedef struct {
  int data[MAX_SIZE];
  int length;
} List;

// Get the value of the i-th element in the list L
bool GetElem(List L, int i, int *e) {
  if (i < 1 || i > L.length) return false;
  *e = L.data[i - 1];
  return true;
}

// Search for an element equal to the given value e in the list L
bool LocateElem(List L, int e) {
  for (int i = 0; i < L.length; i++) {
    if (L.data[i] == e) return true;
  }
  return false;
}

// Insert element e into the i-th position of the list L
bool ListInsert(List *L, int i, int e) {
  if (i < 1 || i > L->length + 1 || L->length == MAX_SIZE) return false;
  for (int j = L->length; j >= i; j--) {
    L->data[j] = L->data[j - 1];
  }
  L->data[i - 1] = e;
  L->length++;
  return true;
}

// Merge two sets A and B, and store the result in A
void unionSet(List *La, List Lb) {
  int La_len = La->length;
  int Lb_len = Lb.length;
  int e;

  for (int i = 1; i <= Lb_len; i++) {
    GetElem(Lb, i, &e);
    if (!LocateElem(*La, e)) {
      ListInsert(La, ++La_len, e);
    }
  }
}

int main() {
  List La, Lb;
  int num;

  printf("Enter the number of elements in set A: ");
  scanf("%d", &num);
  for (int i = 0; i < num; i++) {
    scanf("%d", &La.data[i]);
  }
  La.length = num;

  printf("Enter the number of elements in set B: ");
  scanf("%d", &num);
  for (int i = 0; i < num; i++) {
    scanf("%d", &Lb.data[i]);
  }
  Lb.length = num;

  if (La.length == 0 && Lb.length == 0) {
    printf("ERROR");
    return 0;
  }

  unionSet(&La, Lb);

  for (int i = 0; i < La.length; i++) {
    printf("%d ", La.data[i]);
  }
  printf("\n");

  return 0;
}
